const app = getApp()
import trigger from '../../utils/util.js'
Page({
	options: {},
	data: {
		audio_off: ' ',
		p_name: '',
		g_iphone: '',
		data_num: '',
		audioAction: {
			method: 'pause'
		},
		qian: 0,
		bu: '',
		gender: '',
		getinfobutton: true,
		zou_lu_go: '',
		on_wise: 'on',
		ren_wu: '',
		gold_type: '',
		video_off: '',
		video_type: true,
		mei_on: '',
		button_suo: 'suo',
		mei_list: [],
		qian_num: '0',
		video_src: '',
		up_on: '',
		gif_on: '',
		skin_on: '',
		tiao_off: 'off',
		nav_on_1:'on',
        todaystep:'加载中',
        isRead: true
	},
	scroll_click:function(){
		if(this.scroll){
			wx.pageScrollTo({
				scrollTop: 9999,
				duration: 300
			});
			this.scroll = false;
		}else{
			wx.pageScrollTo({
				scrollTop: 0,
				duration: 300
			});
			this.scroll = true;
		}
		this.setData({
			scroll_on:this.scroll ? '':'on',
		});
	},
	Notice_click:function(){
		this.setData({
			Notice_on:'on'
		})
	},
	Notice_off:function(){
		this.setData({
			Notice_on:''
		})
	},
	get_stepRatio:function(){
		app.request(app.apiDomain + "index.php?c=user&a=stepRatio", {}, function(res) {});	
	},
	gif_off: function() {
		this.setData({
			gif_on: ''
		})
	},
	tui_jian_out: function() {
		this.setData({
			tui_jian_on: ''
		})
	},
	skin_click: function() {
		//储存皮肤
		if (this.data.skin_on == 'on') {
			this.setData({
				skin_on: ''
			})
		} else {
			this.setData({
				skin_on: 'on'
			})
		}
	},
	skin_li_click: function(e) {
		// console.log(e.currentTarget)
		var wrds = e.currentTarget.dataset;
		wx.setStorage({
			key: "skin_type",
			data: wrds.id,
		})
		wx.setStorage({
			key: "skin_font",
			data: wrds.font,
		})
		wx.setStorage({
			key: "skin_color",
			data: wrds.color,
		})
		this.setData({
			skin: wrds.id,
			skin_li: wrds.id
		})
		wx.setNavigationBarColor({
			frontColor: wrds.font,
			backgroundColor: wrds.color,
			animation: {
				duration: 300,
				timingFunc: 'easeIn'
			}
		})
		if (wrds.id == 'type_2') {
			this.mp3 = 'http://resource.160nurse.com/canvas/healthy/mp3/bgm_2.mp4'
		} else {
			this.mp3 = 'http://resource.160nurse.com/canvas/healthy/mp3/bgm.mp4'
		}
		let vid = wx.getStorageSync('try_video');
		if (vid == 'true') {
			this.videoContext = wx.createVideoContext('myVideo')
			this.setData({
				video_src: this.mp3
			});
			this.videoContext.play();
			console.log('开始播放')
		}
		//
		if (this.data.skin_on == 'on') {
			this.setData({
				skin_on: ''
			})
		} else {
			this.setData({
				skin_on: 'on'
			})
		}
	},
	li_click: function(e) {
		var a = e.target.dataset.item //题目
		var b = e.target.dataset.list //选择
		var id = this.data.mei_list[a].id //ID
		var c = parseFloat(this.data.mei_list[a].answer) - 1;
		var thas = this;
		// console.log(a, b, c, id);
		//判断对错
		if (b == c) {
			var yes = 'on'; //对
			//跟服务器说他做对了
			app.request(app.apiDomain + "index.php?c=health&a=practise", {
				id: id
			}, function(res) {
				console.log(res.data.info);
				if (res.data.status == 1) {
					thas.qian_num++
					thas.setData({
						qian: (parseFloat(thas.data.qian * 10) + 1) / 10,
					})
				}
			});
		} else {
			var yes = 'off'; //错
			setTimeout(function(e = thas, d = a, g = c) {
				e.setData({
					['mei_list[' + d + '].list[' + g + '].yes']: 'on',
				})
			}, 400);
		};
		this.setData({
			['mei_list[' + a + '].list[' + b + '].yes']: yes,
			['mei_list[' + a + '].div_suo']: 'suo', //锁按钮
			button_suo: '',
		})
		// 			setTimeout(function(e=thas){
		// 					e.setData({
		// 							up_on:''
		// 					})
		// 			},300);

	},
	ti_mu_click: function(e) {
		this.width_left = this.width_left + 80;
		this.setData({
			button_suo: 'suo',
			wid_left: '-' + this.width_left + 'vw'
		});
		if (this.width_left == 480) {
			this.setData({
				jie_guo: 'on',
				mei_on: '',
				qian_num: (this.qian_num / 10),
			})
		};
	},
	mei_click: function() {
		//获取题目
		var that = this;
		//储存答题的钱
		this.qian_num = 0;
		app.request(app.apiDomain + "index.php?c=health&a=getPaperContext", {}, function(res) {
			console.log(res.data.info);
			that.setData({
				mei_list: res.data.data
			})
		});
		//储存题目位置
		this.width_left = 0;
		this.setData({
			mei_on: 'on',
			jie_guo: '',
			wid_left: '0'
		})
	},
	mei_out: function() {
		this.setData({
			mei_on: '',
			jie_guo: ''
		})
	},
	stop_anim: function() {
		var has = this;
		this.setData({
			zou_lu_go: 'stop'
		})
		setTimeout(function() {
			has.setData({
				zou_lu_go: 'go'
			})
		}, 2000);
	},
	ren_click: function() {
		if (this.ren_true) {
			this.ren_true = false;
			var has = this;
			this.setData({
				ren_wu: 'amin'
			})
			setTimeout(function(e = has) {
				has.setData({
					ren_wu: 'amin_2'
				})
				e.ren_true = true;
			}, 4000);
			const innerAudioContext = wx.createInnerAudioContext('xiong')
			innerAudioContext.autoplay = true
			innerAudioContext.src = 'http://resource.160nurse.com/canvas/healthy/mp3/ling_qu.mp3'
			innerAudioContext.onPlay(() => {
				console.log('开始播放')
			});
		}
	},
	onReady: function() {
		app.SystemInfo(this)
		// var thas=this;
		// setTimeout(function(e=thas){
		// 	e.setData({
		// 		index_load_on:'on'
		// 	})
		// 	setTimeout(function(thas=e){
		// 		e.setData({
		// 			index_load_none:'none',
		// 			ren_wu_on:'on'
		// 		})
		// 	},300);
		// },1700);
		
	},
	formSubmit: function(e) {
		//获取金币
		console.log(e.detail.target.dataset.num)
		app.request(app.apiDomain + "index.php?c=point&a=steprecord", {
			point_step: e.detail.target.dataset.num * 1000,
			point: e.detail.target.dataset.num,
			form_id: e.detail.formId,
		}, function(res) {
			console.log('获取成功');
		});
		var a = parseFloat(this.data.qian) + parseFloat(e.detail.target.dataset.num);
		// console.log(a)
		this.p_name += 'p_' + e.detail.target.dataset.id + ' ';
		this.setData({
			p_name: this.p_name,
			qian: a.toFixed(1),
			// 				audioAction: {
			// 					method: 'play'
			// 				}
		})
		const innerAudioContext = wx.createInnerAudioContext('qian_1')
		innerAudioContext.autoplay = true
		innerAudioContext.src = 'http://resource.160nurse.com/canvas/healthy/img/zou_lu/qian.mp3'
		innerAudioContext.onPlay(() => {
			console.log('开始播放')
		});
	},
	form_id_get: function(e) {
		app.request(app.apiDomain + "index.php?c=user&a=addFormId", {
			type:'form_id',
			form_id: e.detail.formId,
		}, function(res) {
			console.log(res.data.info);
		});
	},
	click_1: function() {
		this.setData({
			gender: 'on'
		})
    },
    click_2: function() {
        wx.navigateTo({
            url: '/pages/skin/index'
        })
    },
	bg_click: function() {
		this.setData({
			gender: ' '
		})
	},
	runSettingBack: function() {
		this.setData({
			rundatabutton: false
		});
		this.onLoad({
			state: 0,
			invite_user: this.options.user_id
		});
	},
	badge_off:function(){
		this.setData({
			badge_on:''
		})
		// this.$set(this.badge_on,'aoe',25)
		// this.badge_on = '123'
	},
	onlogin: function() {
		wx.showToast({
			title: '登陆异常，请关闭小程序并重新进入',
			icon: "none"
		});
	},
	onLoad: function(options) {
        // console.log(options);
        this.options = options
    // app.checkSession(this, '', '', '', '', '', '', '', '')
        console.log('options', this.options)
		this.scroll = true;
		//
        this.rouse_user_id = options.rouse_user_id?options.rouse_user_id:'';
		var that = this;
		
		//查询是否有徽章
		// var badgeTime = Date.parse(new Date());  
		// badgeTime = badgeTime / 1000;
		// if(wx.getStorageSync('badgeTime') < (badgeTime-10)){//86400
			app.request(app.apiDomain + 'index.php?c=user&a=queryMedal', {
			}, function(res) {
				console.log(res.data.info)
				if(res.data.status == 1){
					that.setData({
						badge_on:'on',
						badge_src:'//resource.160nurse.com/'+res.data.data.medal.image_c,
						badge_id:res.data.data.medal.id
					})
					setTimeout(function(e=that){
						e.setData({
							badge_get:'get'
						});
						var g=e
						setTimeout(function(f=g){
							e.setData({
								badge_hua:'hua'
							});
						},300)
					},1200)
				};
				// wx.setStorageSync('badgeTime',badgeTime)
			});
		// }else{
		// 	console.log('今天已经弹过了')
		// }
		//判断皮肤
		if(!wx.getStorageSync('skin_num')){
			console.log(wx.getStorageSync('skin_num'))
			wx.setStorage({key: "skin_num",data: 1,})
			wx.setStorage({key: "skin_type",data:'type_3',})
			wx.setStorage({key: "skin_font",data:'#ffffff',})
			wx.setStorage({key: "skin_color",data:'#9ec3fb',})
			var skin = 'type_3';
			var skin_font = '#ffffff';
			var skin_color = '#9ec3fb';
		}else{
			console.log(wx.getStorageSync('skin_num'),123)
			var skin = wx.getStorageSync('skin_type');
			var skin_font = wx.getStorageSync('skin_font');
			var skin_color = wx.getStorageSync('skin_color');
		};
		
		//储存邀请人id
		if(options.user_id){
			console.log('已储存邀请人id')
			wx.setStorageSync('invite_user_user_id',options.user_id);
		}
		
		//授权
		// app.checkSession(this,'','','','','','',true,'true');
    (trigger.trigger)(this)
		
		// console.log(options.new_url,'传送成功')
		//第一次进来
		if(options.new_url){
			that.setData({
				gif_on:'on'
			})
		}
		//判断第一次进来
		if(!wx.getStorageSync('Notice')){
			this.setData({
				Notice_on:''
			})
			wx.setStorageSync('Notice','1');
		}
		
		if (!skin) {
			skin = 'type_3';
			skin_font = '#ffffff';
			skin_color = '#9ec3fb';
			console.log('默认')
		};
		wx.setNavigationBarColor({
			frontColor: skin_font, //skin_font
			backgroundColor: skin_color, //skin_color
			animation: {
				duration: 300,
				timingFunc: 'easeIn'
			}
		})
		this.setData({
			skin: skin,
			skin_li: skin,
		});
		//获取推荐商品  送积分
		app.request(app.apiDomain + 'index.php?c=mall&a=recommend', {
			// test:'1'
		}, function(res) {
			var a = res.data.status;
			// console.log(a)
			that.setData({
				tui_jian: res.data.data,
				tui_jian_on: a == 1 ? 'on' : ''
			})
		});
		//
		this.options = options;
		// console.log(options);
		//判断是否是从画布过来的
		if (options.state) {
			this.setData({
				gender: 'on'
			})
		}
		//储存答题的钱
		this.qian_num = 0;
		//
		this.ren_true = true;
		//
		let vid = wx.getStorageSync('try_video');
		if (wx.getStorageSync('skin_type') == 'type_2' || !wx.getStorageSync('skin_type')) {
			this.mp3 = 'http://resource.160nurse.com/canvas/healthy/mp3/bgm_2.mp4'
		} else {
			this.mp3 = 'http://resource.160nurse.com/canvas/healthy/mp3/bgm.mp4'
		}
		if (vid == 'true') {
			this.videoContext = wx.createVideoContext('myVideo')
			this.setData({
				video_src: this.mp3
			});
			this.videoContext.play();
			console.log('开始播放')
		} else {
			this.videoContext = wx.createVideoContext('myVideo')
			this.videoContext.pause();
			console.log('暂停播放')
			this.setData({
				video_type: false,
				video_off: 'off'
			});
		}

		this.audio_true = false;
		this.p_name = '';

        // 判断是否有未读消息
        app.request(app.apiDomain+"index.php?c=message&a=isRead",{
            rouse_user_id: that.rouse_user_id
        },res=>{
            // console.log(res);
            if(res.data.data==1){
                //有未读消息
                that.setData({
                    isRead: false
                })
            }else if(res.data.data==0){
                // 没有未读消息
                that.setData({
                    isRead: true
                })
            }
        })
		// 		const tach=this
		// 		setTimeout(function(e=tach){
		// 			// console.log(e)
		// 			e.setData({
		// 				zou_lu_go:'go'
		// 			});
		// 		},3000);

		//player()
		
		//获取题目
		// index.php?c=health&a=getPaperContext&user_id=1232
		

    },
    showNotice(){
        wx.navigateTo({
            url: '../personal/msgTip'
        })
    },
	video_click: function(e) {
		console.log(this.mp3)
		this.video = wx.createVideoContext('myVideo');
		// videoContextCurrent
		if (this.data.video_type) { // 没有播放时播放视频
			this.video.pause()
			this.setData({
				video_type: false,
				video_off: 'off'
			});
			wx.setStorage({
				key: "try_video",
				data: "false"
			})
		} else {
			this.setData({
				video_type: true,
				video_off: '',
				video_src: this.mp3,
			});
			this.video.play();
			wx.setStorage({
				key: "try_video",
				data: "true"
			})
		}
	},
	tiao_click: function() {
		if (this.data.tiao_off == 'off') {
			this.setData({
				tiao_off: ''
			})
		} else {
			this.setData({
				tiao_off: 'off'
			})
		}
	},
	

	/**
	 * 步数记录
	 */
	stepRecord: function() {
		var step = 1000;
		app.request(app.apiDomain + 'index.php?c=point&a=steprecord', {
			step: step, //第多少步
			point: point //步数对应的积分
		}, function(res) {
			//记录是否成功
		});
	},
	/**
	 * 生命周期函数--监听页面卸载
	 */
	onShow: function() {
		var tha = this
    console.log(this)
    // (trigger.trigger)(this)
    app.checkSession(this, '', '', '', '', '', '', true, 'true');
    // tha.setData({
    //   run_auth: wx.getStorageSync('run_auth')
    // })
    // console.log('run_auth', tha.data.run_auth)
		var date = new Date();
		var today = date.getFullYear() + '-' + date.getMonth() + '-' + date.getDate();
		var point_str = wx.getStorageSync("checkin_point");
		var point = point_str.split(':');
		var sleepPoint = 0;
		if (point[0] == today) {
			sleepPoint = point[1];
		}
		this.setData({
			sleepPoint: sleepPoint,
		});

		//判断是否第一次登陆
		if (wx.getStorageSync('ren_no1')) {
			//动画
			setTimeout(function(e = tha) {
				e.setData({
					zou_lu_go: 'go',
				});
			}, 500);
		} else {
			wx.setStorage({
				key: 'ren_no1',
				data: 'true'
			})
			//动画
			setTimeout(function(e = tha) {
				e.setData({
					zou_lu_go: 'go',
					gold_type: 'on' //显示新题
				});
			}, 4000);
		};
		//显示新题
		setTimeout(function(e = tha) {
			e.setData({
				gold_type: 'on'
			});
		}, 1500);


	},
	onHide: function() {
		wx.setStorage({
			key: "try_video",
			data: "false"
		})
		// console.log(vid)
		this.setData({
			zou_lu_go: '',
			video_type: false,
			video_off: 'off'
		});
		this.videoContext.pause();
	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {},
	getUserInfoPlay: function(e) {
		this.getUserInfo();
	},
	getUserInfoGrow: function(e) {
		this.getUserInfo();
		wx.navigateTo({
			url: '../grow/index',
		});
	},
	getUserInfoEat: function(e) {
		this.getUserInfo();
		wx.navigateTo({
			url: '../eat_index/index',
		});
	},
	getUserInfoNews: function(e) {
		this.getUserInfo();
		wx.navigateTo({
			url: '../news/index?type=101120101',
		});
	},
	getUserInfoReward: function(e) {
		this.getUserInfo();
		wx.navigateTo({
			url: '../complete/xu_yuan',
		});
	},
	float_3_click:function(){
		wx.showToast({
		  title: '趣味健康活动升级中，敬请期待。',
		  icon: 'none',
		  duration: 2000
		})
	},
  run_auth: function (openid,e) {
    wx.openSetting({
      
    })
  },
  getUserInfo: function () {
    var that = this
    if (app.getUserInfo(that, this.options)) {
      that.setData({
        user_auth: false
      })
    }
  },
	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {
    let that = this
    app.getRunData(wx.getStorageSync('openid'),that)
    // that.formSubmit()
	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {},

	onShareAppMessage: function() {
		return app.onShareAppMessage();
	},
	Loaded:function(){
		console.log('页面加载完成')
	}
})
